﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace V_League.DataAccess
{
    public class clsConnect
    {
        SqlConnection sqlCon;
        String connectionstring = @"Data source=PHUTHUAN\SQLEXPRESS;Database=QuanLyBongDa;Trusted_Connection=True";
        SqlCommand sqlCmd;
        SqlDataAdapter adapter;
        SqlDataReader reader;
        DataTable dt;

        public clsConnect()
        {
            sqlCon = new SqlConnection(connectionstring);
            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Ex: " + e.ToString());
            }
        }

        public void Disconnect()
        {
            try
            {
                sqlCon.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Ex : " + e.ToString());
            }
        }

        public DataTable LoadData(String sql)
        {
            sqlCmd = new SqlCommand(sql, sqlCon);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            adapter = new SqlDataAdapter(sqlCmd);
            dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }

        public DataTable LoadData(String sql, String[] name, object[] value, int Nparameters)
        {
            sqlCmd = new SqlCommand(sql, sqlCon);
            for (int i = 0; i < Nparameters; i++)
            {
                sqlCmd.Parameters.AddWithValue(name[i], value[i]);
            }
            sqlCmd.CommandType = CommandType.StoredProcedure;
            adapter = new SqlDataAdapter(sqlCmd);
            dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }

        public void ExecuteNonQuery(String sql)
        {
            sqlCmd = new SqlCommand(sql, sqlCon);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.ExecuteNonQuery();
        }

        public void ExecuteNonQuery(String sql, String[] name, object[] value, int nParameter)
        {
            sqlCmd = new SqlCommand(sql, sqlCon);
            for (int i = 0; i < nParameter; i++)
            {
                sqlCmd.Parameters.AddWithValue(name[i], value[i]);
            }
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.ExecuteNonQuery();
        }

        public SqlDataReader ExecuteDataReader(String sql)
        {
            sqlCmd = new SqlCommand(sql, sqlCon);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            return sqlCmd.ExecuteReader();
        }

        public SqlDataReader ExecuteDataReader(String sql, String[] name, object[] value, int nParameter)
        {
            sqlCmd = new SqlCommand(sql, sqlCon);
            for (int i = 0; i < nParameter; i++)
            {
                sqlCmd.Parameters.AddWithValue(name[i], value[i]);
            }
            sqlCmd.CommandType = CommandType.StoredProcedure;
            return sqlCmd.ExecuteReader();
        }

        public String GetID(String ID_Text, int ID_length, String sql)
        {
            int maxID;
            int length_no;

            sqlCmd = new SqlCommand(sql, sqlCon);
            sqlCmd.CommandType = CommandType.StoredProcedure;

            reader = sqlCmd.ExecuteReader();

            String ID = ID_Text;

            while (reader.Read())
            {
                try
                {
                    maxID = int.Parse(reader[0].ToString().Substring(ID_Text.Length));
                    maxID++;
                }
                catch
                {
                    maxID = 1;
                }

                length_no = maxID.ToString().Length;

                for (int i = 0; i < ID_length - length_no; i++)
                {
                    ID += "0";
                }

                ID += maxID.ToString();
            }
            return ID;
        }
    }
}
